package com.zjd.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.entity.ActiveLogs;
import com.zjd.association.entity.Activities;
import com.zjd.association.entity.Teams;
import com.zjd.association.mapper.ActiveLogsMapper;
import com.zjd.association.mapper.ActivitiesMapper;
import com.zjd.association.mapper.TeamsMapper;
import com.zjd.association.service.ActivitiesService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 活动活动 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class ActivitiesServiceImpl implements ActivitiesService {

    private final TeamsMapper teamsMapper;

    private final ActiveLogsMapper activeLogsMapper;

    private final ActivitiesMapper activitiesMapper;

    public void add(Activities activities) {
        activitiesMapper.insert(activities);
        Teams teams = teamsMapper.selectById(activities.getTeamId());
        ActiveLogs activeLog = new ActiveLogs();
        activeLog.setId(UUIDUtils.makeIDByCurrent());
        activeLog.setActiveId(activities.getId());
        activeLog.setUserId(teams.getManager());
        activeLog.setCreateTime(DateUtils.getNowDate());
        activeLogsMapper.insert(activeLog);
    }

    @Override
    public void update(Activities activities) {
        activitiesMapper.updateById(activities);
    }

    @Override
    public void delete(Activities activities) {
        QueryWrapper<ActiveLogs> qw = new QueryWrapper<>();
        qw.eq("active_id", activities.getId());
        activeLogsMapper.delete(qw);
        activitiesMapper.deleteById(activities);
    }

    @Override
    public Activities getOne(String id) {
        return activitiesMapper.selectById(id);
    }

    /**
     * 分页查询活动信息
     *
     * @param pageIndex  当前页码
     * @param pageSize   每页数据量
     * @param activeName 活动名称
     * @param teamName   团队名称
     * @return 活动信息
     */
    @Override
    public PageWrapper getPageAll(Long pageIndex, Long pageSize, String activeName, String teamName) {
        Page<Map<String, Object>> page =
                activitiesMapper.qryPageAll(new Page<>(pageIndex, pageSize), activeName, teamName);
        return PageWrapper.parsePage(page);
    }

    /**
     * 分页查询指定成员相关活动信息
     *
     * @param pageIndex  当前页码
     * @param pageSize   每页数据量
     * @param userId     指定成员ID
     * @param activeName 活动名称
     * @param teamName   团队名称
     * @return 活动信息
     */
    @Override
    public PageWrapper getPageByUserId(Long pageIndex, Long pageSize, String userId, String activeName, String teamName) {
        Page<Map<String, Object>> page =
                activitiesMapper.qryPageByMemId(new Page<>(pageIndex, pageSize), userId, activeName, teamName);
        return PageWrapper.parsePage(page);
    }

}